#include <LCUI.h>
#include <LCUI/gui/widget.h>
#include <LCUI/gui/builder.h>
#include "version.h"
#include "ui.h"
#include <LCUI/gui/widget/textview.h>
#include <LCUI/gui/widget/textedit.h>
#include <string.h>
#include <stdlib.h>
#include <LCUI/util/charset.h>
#include <stdint.h>
#include <stdio.h>
#include <LCUI/font.h>
int dp[110][55][55];
int b[55][55];
int c[100];
void paint_border(LCUI_PaintContext paint, LCUI_Rect *box)
{
	LCUI_Border border = { 0 };
	LCUI_Color black = RGB(0, 0, 0);

	border.top.color = black;
	border.top.style = SV_SOLID;
	border.top.width = 4;
	border.right.color = black;
	border.right.style = SV_SOLID;
	border.right.width = 4;
	border.bottom.color = black;
	border.bottom.style = SV_SOLID;
	border.bottom.width = 4;
	border.left.color = black;
	border.left.style = SV_SOLID;
	border.left.width = 4;
	border.top_left_radius = 32;
	border.top_right_radius = 32;
	border.bottom_left_radius = 32;
	border.bottom_right_radius = 32;
	Border_Paint(&border, box, paint);
}
wchar_t *DecodeUTF8(const char *str)
{
	size_t len = strlen(str) + 1;
	wchar_t *wstr = malloc(len * sizeof(wchar_t));
	LCUI_DecodeString(wstr, str, len, ENCODING_UTF8);
	return wstr;
}
void OnButtonClick(LCUI_Widget self, LCUI_WidgetEvent e, void *arg)
{
	wchar_t str[256];
	wchar_t str2[256];
	wchar_t str3[256];
	LCUI_Widget texth = LCUIWidget_GetById("texth");
	LCUI_Widget textl = LCUIWidget_GetById("textl");
	LCUI_Widget inputh = LCUIWidget_GetById("inputh");
	LCUI_Widget inputl = LCUIWidget_GetById("inputl");
	LCUI_Widget inputall = LCUIWidget_GetById("inputall");

	TextEdit_GetTextW(inputh, 0, 255, str);

	TextEdit_GetTextW(inputl, 0, 255, str2);

	TextEdit_GetTextW(inputall, 0, 255, str3);
	DWORD dBufSize =
	    WideCharToMultiByte(CP_OEMCP, 0, str3, -1, NULL, 0, NULL, FALSE);

	//分配目标缓存
	char *dBuf = malloc(sizeof(char) * dBufSize);
	memset(dBuf, 0, dBufSize);

	//转换
	int nRet = WideCharToMultiByte(CP_OEMCP, 0, str3, -1, dBuf, dBufSize,
				       NULL, FALSE);

	//第一个str 行的转变
	DWORD dBufSize1 =
	    WideCharToMultiByte(CP_OEMCP, 0, str, -1, NULL, 0, NULL, FALSE);

	//分配目标缓存
	char *dBuf1 = malloc(sizeof(char) * dBufSize1);
	memset(dBuf1, 0, dBufSize1);

	//转换
	int nRet1 = WideCharToMultiByte(CP_OEMCP, 0, str, -1, dBuf1, dBufSize1,
					NULL, FALSE);

	//第二个str2 列的转换
	DWORD dBufSize2 =
	    WideCharToMultiByte(CP_OEMCP, 0, str2, -1, NULL, 0, NULL, FALSE);

	//分配目标缓存
	char *dBuf2 = malloc(sizeof(char) * dBufSize2);
	memset(dBuf2, 0, dBufSize2);

	//转换
	int nRet2 = WideCharToMultiByte(CP_OEMCP, 0, str2, -1, dBuf2, dBufSize2,
					NULL, FALSE);

	if (nRet <= 0) {
		TextView_SetTextW(texth, str);
	} else {
		int len = strlen(dBuf);
		int len1 = strlen(dBuf1);
		int len2 = strlen(dBuf2);
		if (len == 0 || len1 == 0 || len2 == 0) {
			return;
		}

		int *nums = malloc(sizeof(int) * 100010);
		int *nums1 = malloc(sizeof(int) * 100010);
		int *nums2 = malloc(sizeof(int) * 100010);
		//将字符数组转化成整数数组
		//行的转化
		int k1 = 0;
		char *token1 = strtok(dBuf1, " ");
		while (token1 != NULL) {
			nums1[k1] = atoi(token1);
			token1 = strtok(NULL, " ");
			k1++;
		}
		//列的转化
		int k2 = 0;
		char *token2 = strtok(dBuf2, " ");
		while (token2 != NULL) {
			nums2[k2] = atoi(token2);
			token2 = strtok(NULL, " ");
			k2++;
		}
		//输入的内容的转化
		int k = 0;
		char *token = strtok(dBuf, " ");
		while (token != NULL) {
			nums[k] = atoi(token);
			token = strtok(NULL, " ");
			k++;
		}

		int a[55][55];
		int m = 0;
		for (int i = 1; i <= nums1[0]; i++) {
			for (int j = 1; j <= nums2[0]; j++) {
				a[i][j] = nums[m];
				m++;
			}
		}

		memset(dp, 0, sizeof(dp));
		int kp = nums1[0] + nums2[0];
		int row = nums1[0];
		int col = nums2[0];
		//绘制图形要进行做的标记

		memset(b, 0, sizeof(b));
		memset(c, 0, sizeof(c));
		int h = 0;
		for (int l = 2; l < kp; l++) {
			for (int i = 1; i <= row; i++) {
				for (int j = 1; j <= row; j++) {
					if (i != j && l >= i && l >= j) {
						dp[l][i][j] =
						    max(max(dp[l - 1][i - 1][j],
							    dp[l - 1][i]
							      [j - 1]),
							max(dp[l - 1][i - 1]
							      [j - 1],
							    dp[l - 1][i][j])) +
						    a[i][l - i] + a[j][l - j];
						if (dp[l - 1][i - 1][j] ==
						    dp[l][i][j] - a[i][l - i] -
							a[j][l - j]) {
							b[i - 1]
							 [l - 1 - (i - 1)] = 1;
							b[j][l - 1 - j] = 2;
						} else if (dp[l - 1][i]
							     [j - 1] ==
							   dp[l][i][j] -
							       a[i][l - i] -
							       a[j][l - j]) {
							b[i][l - 1 - i] = 1;
							b[j - 1]
							 [l - 1 - j + 1] = 2;
						} else if (dp[l - 1][i - 1]
							     [j - 1] ==
							   dp[l][i][j] -
							       a[i][l - i] -
							       a[j][l - j]) {
							b[i - 1]
							 [l - 1 - i + 1] = 1;
							b[j - 1]
							 [l - 1 - j + 1] = 2;
						} else if (dp[l - 1][i][j] ==
							   dp[l][i][j] -
							       a[i][l - i] -
							       a[j][l - j]) {
							b[i][l - 1 - i] = 1;
							b[j][l - 1 - j] = 2;
						}
					}
				}
			}
		}
		dp[kp][row][row] =
		    max(dp[kp - 1][row][row - 1], dp[kp - 1][row - 1][row]) +
		    a[row][col];
		if (dp[kp - 1][row][row - 1] ==
		    dp[kp][row][row] - a[row][col]) {
			b[row][kp - 1 - row] = 1;
			b[row - 1][kp - 1 - row + 1] = 2;
		} else if (dp[kp - 1][row - 1][row] ==
			   dp[kp][row][row] - a[row][col]) {
			b[row - 1][kp - 1 - row + 1] = 1;
			b[row][kp - 1 - row] = 2;
		}

		b[row][kp - row] = 3;
		b[1][1] = 3;

		char *finally = malloc(sizeof(char) * 256);
		_itoa(dp[kp][row][row], finally, 10);
		int sBufSize = strlen(finally);
		//获取输出缓存大小
		// VC++ 默认使用ANSI，故取第一个参数为CP_ACP
		DWORD dBufSize =
		    MultiByteToWideChar(CP_ACP, 0, finally, 10, NULL, 0);
		printf("需要wchar_t%u个\n", dBufSize);

		wchar_t *dBuf = malloc(sizeof(wchar_t) * 10010);
		wmemset(dBuf, 0, dBufSize);

		//进行转换
		int nRet = MultiByteToWideChar(CP_ACP, 0, finally, sBufSize,
					       dBuf, dBufSize);

		TextView_SetTextW(textl, dBuf);

		//进行最终图形的绘制
		int border_size = 4;
		int i, j;
		LCUI_Graph canvas;
		LCUI_Color color;
		LCUI_Rect rect;

		Graph_Init(&canvas);
		Graph_Create(&canvas, 150, 150);
		for (i = 1; i <= nums1[0]; ++i) {
			for (j = 1; j <= nums2[0]; ++j) {
				if (b[i][j] == 1) {
					color.red = 255;
					color.green = 0;
					color.blue = 0;
					/* code */
				} else if (b[i][j] == 2) {
					color.red = 0;
					color.green = 255;
					color.blue = 0;
					/* code */
				} else if (b[i][j] == 3) {
					color.red = 0;
					color.green = 0;
					color.blue = 255;
					/* code */
				} else if (b[i][j] == 0) {
					color.red = 255;
					color.green = 255;
					color.blue = 255;
				}

				rect.x = j * 25;
				rect.y = i * 25;
				rect.width = 25;
				rect.height = 25;

				Graph_FillRect(&canvas, color, &rect, FALSE);
			}
		}
		LCUI_WritePNGFile("test_fill_rect.png", &canvas);

		Graph_Free(&canvas);
		//
		// int ret, fid;

		// LCUI_InitFontLibrary();
		// ret = LCUIFont_LoadFile("C:/Windows/fonts/simhei.ttf");
		// LCUI_FontBitmap bmp;
		// while (ret == 0) {
		// 	fid = LCUIFont_GetId("Simhei", 0, 0);
		// 	if (fid < 0) {
		// 		break;
		// 	}
		// 	ret = LCUIFont_RenderBitmap(&bmp, L"12", fid, 48);
		// 	if (ret != 0) {
		// 		TextView_SetTextW(texth, str);
		// 		break;
		// 		/* code */
		// 	}
		// 	LCUI_Pos pos = { j * 25, i * 25 };
		// 	LCUI_Color c1 = RGB(138, 251, 4);
		// 	Graph_FillRect(&canvas, color, &rect, FALSE);

		// 	FontBitmap_Mix(&canvas, pos, &bmp, c1);
		// 	FontBitmap_Free(&bmp);
		// 	ret = -1;
		// }

		LCUI_Widget text1;
		text1 = LCUIWidget_GetById("imageout");
		Widget_SetStyle(text1, key_width, 1000, px);
		Widget_SetStyle(text1, key_height, 1000, px);
		LCUI_Graph wpath;
		Graph_Init(&wpath);
		if (LCUI_ReadImageFile("test_fill_rect.png", &wpath) != 0) {
			return -1;
		}
		Widget_SetStyle(text1, key_background_image, &wpath, image);
		Graph_Free(&wpath);
	}
}

int UI_Init(void)
{
	LCUI_Widget root;
	LCUI_Widget wrapper;
	LCUI_Widget button;

	LCUI_Init();
	// UI_InitComponents();
	UI_InitViews();
	wrapper = LCUIBuilder_LoadFile("assets/views/app.xml");
	if (!wrapper) {
		return -1;
	}
	root = LCUIWidget_GetRoot();
	Widget_SetTitleW(root, APP_NAME);
	Widget_Append(root, wrapper);
	Widget_Unwrap(wrapper);
	button = LCUIWidget_GetById("button");
	Widget_BindEvent(button, "click", OnButtonClick, NULL, NULL);

	return 0;
}

int UI_Run(void)
{
	return LCUI_Main();
}
